package com.runjian.rundo.iot.agent.iec104.module;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.runjian.rundo.iot.agent.common.entity.AgentProtocolDTO;

import com.runjian.rundo.iot.agent.iec104.consts.Constans;
import lombok.Getter;
import lombok.Setter;

/**
 * iec104协议配置
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public class Iec104Protocol extends AgentProtocolDTO {

    /**
     * 协议Id
     */
    String id;

    /**
     * 协议名称
     */
    String name;

    /**
     * 属性集
     */
    List<Iec104ProtocolSeries> series;

    /**
     * 属性集
     */
    Map<String, Map<Integer, Iec104ProtocolSeries>> seriesAddressMap=new HashMap<>();

    /**
     * 属性集
     */
    Map<String, Map<String, Iec104ProtocolSeries>> seriesMap=new HashMap<>();


    public Iec104ProtocolSeries getSeriesAddress(int address,String direction) {
        Map<Integer,Iec104ProtocolSeries> seriesAddressMapTemp=seriesAddressMap.computeIfAbsent(direction, (key)->{
            Map<Integer,Iec104ProtocolSeries> map=series.stream().filter(e->e.direction.equals(key)).collect(Collectors.toMap(Iec104ProtocolSeries::getAddress, p -> p));
            return map;
        });
        return seriesAddressMapTemp.get(address);
    }

    public Iec104ProtocolSeries getSeries(String serieName,String direction) {
        Map<String,Iec104ProtocolSeries> seriesMapTemp=seriesMap.computeIfAbsent(direction, (key)->{
            Map<String,Iec104ProtocolSeries> map=series.stream().filter(e->e.direction.equals(key)).collect(Collectors.toMap(Iec104ProtocolSeries::getSeries, p -> p));
            return map;
        });
        return seriesMapTemp.get(serieName);
    }


}
